﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Data;
using MVCDemo.Models;
using MVCDemo.ViewModel;

namespace MVCDemo.Controllers
{
    public class MessagesController : Controller
    {
        private MVCDemoDb db = new MVCDemoDb();

        // GET: Messages
        public ActionResult Index()
        {
            var vm = new SayViewModel
            {
                Username="一天到晚游泳的鱼",
                MessageViewModel=new List<MessageViewModel>
                {
                    new MessageViewModel
                    {
                        Id=1,
                        Content="明天就像是盒子里的巧克力糖，什么滋味，充满想象^_^",
                        CommentViewModels=new List<CommentViewModel>
                        {
                            new CommentViewModel
                            {
                                FromUsername="胡大发明",
                                ToUsername="胡小发明",
                                Comment="老胡来也，到此一游"
                            },
                            new CommentViewModel
                            {
                                FromUsername="Mr.黄",
                                ToUsername="小小曾",
                                Comment="黄黄到此一游"
                            },
                        }
                    },
                    new MessageViewModel
                    {
                        Id=2,
                        Content="啊哈娘子^_^",
                        CommentViewModels=new List<CommentViewModel>
                        {
                            new CommentViewModel
                            {
                                FromUsername="胡大发明",
                                ToUsername="胡小发明",
                                Comment="老胡来也，到此一游"
                            },
                            new CommentViewModel
                            {
                                FromUsername="Mr.黄",
                                ToUsername="小小曾",
                                Comment="黄黄到此一游"
                            },
                        }
                    },
                }
            };


            return View(vm);
        }

        // GET: Messages/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // GET: Messages/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Messages/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性。有关
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,FromUserId,Content,CreatedAt,UpdatedAt,Version,Remarks")] Messages messages)
        {
            if (ModelState.IsValid)
            {
                db.Messages.Add(messages);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(messages);
        }

        // GET: Messages/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // POST: Messages/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性。有关
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,FromUserId,Content,CreatedAt,UpdatedAt,Version,Remarks")] Messages messages)
        {
            if (ModelState.IsValid)
            {
                db.Entry(messages).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(messages);
        }

        // GET: Messages/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // POST: Messages/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Messages messages = db.Messages.Find(id);
            db.Messages.Remove(messages);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
